<?php
class obAction extends Action{
    public function __construct(){
        DB::dbInstall();
    }
    public function indexop(){

        echo "name",'<br>';
        echo "age",'<br>';
       // ob_end_clean();
        echo "29",'<br>';
       // $str = ob_get_contents();
        //file_put_contents('ob_str.txt',$str);
       // header('content-type:text/html;charset=utf-8');

    }
    /*文件缓存原理：先从文件读取，没有文件则从数据库查询并用模板返回，同时在返回前ob获取模板内容并保存在文件（和开始要读取的文件同一文件），下次则从此文件读取。总结：对数据库查询发生在文件不存在或是文件过期*/
    public function flushOp(){
        $cache_file_path = "cache1.html";
        if(is_file($cache_file_path) && filemtime($cache_file_path)+60 > time()){
            include "$cache_file_path";
            exit;
        }

        echo '32<br>';
        /*省略：从数据库查询过程*/
        include TEMPLATES_DIR.DS."ob.html.php";
        $str = ob_get_contents();
        file_put_contents($cache_file_path,$str);
    }
    //以家庭表为案例来完成静态页面生成：添加成员时生成详情静态页
    public function add_staticOp(){
        if(isset($_GET['submit_add_f'])){
        /*添加入库后，根据返回的auto_inc id 命名文件，生成的文件放在Cache/catche_static/fdetail/ */
            $insert_array = array();
            $insert_array['fname'] = $_GET['fname'];
            $insert_array['fage'] = $_GET['fage'];
            $insert_array['fjob'] = $_GET['fjob'];
            $insert_array['frole'] = $_GET['frole'];
            $insert_array['faddr'] = $_GET['faddr'];
            $f_id = DB::Insert('family',$insert_array);
            $file_fdetail_static = CACHE_FDETAIL.DS."fdetail_$f_id.html";
            $tpl_str = file_get_contents(TPLS_DIR.DS."fdetail_tpl.php");
            foreach($insert_array as $k=>$v){
                $tpl_str = str_replace("{".$k."}",$v,$tpl_str);
            }
            if(file_put_contents($file_fdetail_static,$tpl_str)){
               $_SESSION['is_flist_change']=true;
                $this->showMessage('写入文件成功','index.php?act=ob&op=add_static',1);
            }else{
                echo "写入文件失败";
            }

        }else{
            include_once TEMPLATES_DIR.DS."f_add.php";
        }

    }
    public function flistOp(){
        $flist = DB::queryAll("select * from #__family order by fsort");
        include_once TEMPLATES_DIR.DS."f_list.php";
    }

    /**
     *编辑成员入库同时更改对应静态页面
     */
    public function edit_staticOp(){
        if(isset($_GET['submit_edit_f'])){
            $fid = $_GET['fid'];
            $where = "fid=$fid";
            $update_array = array();
            $update_array['fname'] = $_GET['fname'];
            $update_array['fage'] = $_GET['fage'];
            $update_array['fjob'] = $_GET['fjob'];
            $update_array['frole'] = $_GET['frole'];
            $update_array['faddr'] = $_GET['faddr'];
            if(DB::Update('family',$update_array,$where)){
                $file_fdetail_static = CACHE_FDETAIL.DS."fdetail_$fid.html";
                $tpl_str = file_get_contents(TPLS_DIR.DS."fdetail_tpl.php");
                foreach($update_array as $k=>$v){
                    $tpl_str = str_replace("{".$k."}",$v,$tpl_str);
                }
                if(file_put_contents($file_fdetail_static,$tpl_str)){
                    $_SESSION['is_flist_change']=true;
                    $this->showMessage('更新文件成功','index.php?act=ob&op=flist',1);
                }else{
                    echo "更新文件失败";
                }
            }
            die();
        }
        $fid = $_GET['fid'];
        $frow = DB::queryRow("select * from #__family where fid='$fid'");
        include_once TEMPLATES_DIR.DS."f_edit.php";
    }
    public function delOp(){
        $fid = $_GET['fid'];
        $where = "fid='$fid'";
        if(DB::Delete('family',$where)){
            //同时应该删除添加时生成的静态文件
            $file_fdetail_static = CACHE_FDETAIL.DS."fdetail_$fid.html";
            unlink($file_fdetail_static);
            $this->showMessage('删除成员成功(数据库和表态文件都删除)','index.php?act=ob&op=flist',1);
        }else{
            echo 'del-error...';
        }
    }

    //添加或编辑生成对应的详情静态页后，同时生成一个已有成员列表页的静态页
    public function list_staticOp(){
        //$this->redirect(SITE_URL.DS.'index.php?act=fob&op=flist');
        $file_flist_static = CACHE_FDETAIL.DS."flist.html";
        if(is_file($file_flist_static) && !isset($_SESSION['is_flist_change'])){
            echo "<script>alert('已经生成，不用重复操作');</script>";
            exit;
        }

        $flist = DB::queryAll("select * from #__family");
        include_once TPLS_DIR.DS."f_list.php";
        $flist_str = ob_get_contents();
        ob_clean();
        file_put_contents($file_flist_static,$flist_str);
        echo "<script>alert('成功生成最新静态文件');</script>";
        if(isset($_SESSION['is_flist_change'])){
            unset($_SESSION['is_flist_change']);
        }
    }
}